<!-- 新增借支申请 -->
<template>
	<view class="safe-bottom">
		<view class="p-3">
			<view class="bt-block">
				<bg-cell2 label="姓名" :value="$store.getters.name"></bg-cell2>
				<bg-cell2 label="身份证号" :value="$store.state.user.idCard"></bg-cell2>
				<bg-cell2 label="手机号" :value="$store.state.user.phone" :border="false"></bg-cell2>
			</view>
			<view class="my-3 bt-block">
				<bg-cell2 label="选择项目">
					<template slot="value">
						<!-- <u-icon size="9" color="#bebebe" name="arrow-down-fill" label="下拉菜单" labelSize="14"
							labelPos="left"></u-icon> -->
						<bg-select :value.sync="postData.projectId" @select-change="selectHandler" :columns="projects"></bg-select>
					</template>
				</bg-cell2>
				<bg-cell2 :border="false">
					<template slot="label">
						<view class="text-red font-12">
							注：每个项目累计工作天数需超过15天（含）才能进行借支
						</view>
					</template>
				</bg-cell2>
			</view>
			<view class="bt-block my-3">
				<bg-title class="margin-top mx-3">
					<text class="font-14 font-normal">借支申请单</text>
				</bg-title>
				<bg-cell2 label="项目名称" :value="postData.projectName"></bg-cell2>
				<bg-cell2 label="借支金额">
					<template slot="value">
						<view >
							<u--input max-length="10" inputAlign='right' type="number" border="none"
								v-model="postData.amount">
							</u--input>
							<!-- <u-number-box inputWidth="120" v-model="postData.amount" :min="1" :max="10000000" :step="1"></u-number-box> -->
						</view>
					</template>
					
				</bg-cell2>
				<bg-cell2 :border='false'>
					<template slot="label">
						<view class="text-red font-12">
							当前项目最大可借支金额: {{max}}元
						</view>
					</template>
				</bg-cell2>
			</view>
			<view class="bt-block py24px30 my-3">
				<bg-title>
					<text class="font-14 font-normal">本人签字</text>
				</bg-title>
				<view class="mt-3">
					<bg-sign @sign-success="signHandler"></bg-sign>
				</view>
			</view>
		</view>
		<view class="bt-footer">
			<bg-btn @click="saveData()">提交申请借支</bg-btn>
		</view>
	</view>
</template>

<script>
import { borrowProject, addBorrow } from '../../../api/salary'
	export default {
		data() {
			return {
				max:0,
				projects:[],
				postData:{
					"projectId": '',
					"projectName":'',
					"createUserId": this.$store.getters.userId,
					"amount": 1,
					"status": "",
					"signedImg": ""
				}
			}
		},
		mounted() {
			this.getProjects()
		},
		methods: {
			getProjects(){
				borrowProject({}).then(res=>{
					this.projects = res.data.map(item=>{
						return {
							...item,
							label:item.projectName,
							value:item.projectId
						}
					})
				})
			},
			selectHandler({value, values}){
				let _value = value[0]
				this.postData.projectId = _value.projectId
				this.postData.projectName = _value.projectName
				this.max = _value.limitAmount
			},
			signHandler(e){
				this.postData.signedImg = e.fileName
			},
			saveData(){
				if(!this.postData.projectId){
					this.$modal.msg("请选择项目")
					return false
				}
				if(this.postData.amount <=0){
					this.$modal.msg("请输入正确的金额")
					return false
				}
				if(this.postData.amount>this.max){
					this.$modal.msg("不能超过最大可借支金额")
					return false
				}
				if(!this.postData.signedImg){
					this.$modal.msg("请签字")
					return false
				}
				addBorrow(this.postData).then(res=>{
					uni.$emit("refresh-borrow", true)
					this.$modal.msg("您已申请成功，请注意查收入账")

					// uni.showToast({
					// 	title:"您已申请成功，请注意查收入账"
					// })
					setTimeout(()=>{
						uni.navigateBack()
					},800)
				})
			}
		},
		
	}
</script>

<style>

</style>